﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;

namespace FacebookApp
{
	public class UserService
	{

		#region[User_GetbyAll]
		public static List<User> User_GetByAll()
		{
            try
            {
                using (var dt = new DataClassesDataContext())
                {
                    var da = (from pu in dt.Users select pu).ToList();

                    return da;
                }
            }
            catch (Exception)
            {
                return null;
            }
		}
		#endregion
		#region[User_GetbyId]
        public static User User_GetById(string Data)
		{
            try
            {
                using (var dt = new DataClassesDataContext())
                {
                    int id = int.Parse(Data);
                    var da = (from pu in dt.Users where pu.Id == id select pu).First();

                    return da;
                }
            }
            catch (Exception)
            {
                return null;
            }
		}
		#endregion
		#region[User_GetbyTop]
        public static List<User> User_GetByTop(string Top, string Where, string Order)
		{
            try
            {
                using (var dt = new DataClassesDataContext())
                {
                    string query = "Select * from [User]";
                    if (!Top.Equals(""))
                        query = " Select top " + Top + " * from [User] ";
                    if (!Where.Equals(""))
                        query += " where " + Where + " ";
                    if (!Order.Equals(""))
                        query += " order by " + Order;

                    IEnumerable<User> results = dt.ExecuteQuery<User>(@query);

                    return results.ToList();
                }
            }
            catch (Exception e)
            {
                return null;
            }
		}
		#endregion
		#region[User_GetbyGroupUser_Id]
		public static List<User> User_GetByGroupUser_Id(string GroupUser_Id)
		{
            try
            {
                using (var dt = new DataClassesDataContext())
                {
                    int id = int.Parse(GroupUser_Id);
                    var da = (from pu in dt.Users where pu.GroupUser_Id == id select pu).ToList();

                    return da;
                }
            }
            catch (Exception)
            {
                return null;
            }
		}
		#endregion
		#region[User_Insert]
        public static void User_Insert(UserInfo Data)
		{
            try
            {
                using (var dt = new DataClassesDataContext())
                {
                    User ob = new User();
                    ob.FB_Id = Data.FB_Id;
                    ob.UserName = Data.UserName;
                    ob.FullName = Data.FullName;
                    ob.Address = Data.Address;
                    ob.Password = Data.Password;
                    ob.Phone = Data.Phone;
                    ob.Email = Data.Email;
                    ob.Status = bool.Parse(Data.Status);
                    ob.DateLogin = DateTime.Now;
                    ob.GroupUser_Id = long.Parse(Data.GroupUser_Id);

                    dt.Users.InsertOnSubmit(ob);
                    dt.SubmitChanges();
                }
            }
            catch (Exception)
            {

            }
		}
		#endregion
		#region[User_Update]
        public static void User_Update(UserInfo Data)
		{
            try
            {
                using (var dt = new DataClassesDataContext())
                {
                    int id = int.Parse(Data.Id);
                    //Author da = (from au in dt.Authors where au.Id == id select au).First();
                    User ob = dt.Users.Single(au => au.Id == id);
                    ob.FB_Id = Data.FB_Id;
                    ob.UserName = Data.UserName;
                    ob.FullName = Data.FullName;
                    ob.Address = Data.Address;
                    ob.Password = Data.Password;
                    ob.Phone = Data.Phone;
                    ob.Email = Data.Email;
                    ob.Status = bool.Parse(Data.Status);
                    //ob.DateLogin = DateTime.Now;
                    ob.GroupUser_Id = long.Parse(Data.GroupUser_Id);

                    dt.SubmitChanges();
                }
            }
            catch (Exception)
            {

            }
		}
		#endregion
		#region[User_Delete]
        public static void User_Delete(string Data)
		{
            try
            {
                using (var dt = new DataClassesDataContext())
                {
                    int id = int.Parse(Data);
                    //Author da = (from au in dt.Authors where au.Id == id select au).First();
                    User da = dt.Users.Single(au => au.Id == id);
                    dt.Users.DeleteOnSubmit(da);

                    dt.SubmitChanges();
                }
            }
            catch (Exception)
            {

            }
		}
		#endregion
        #region[User_CheckLogin]
        public static User User_CheckLogin(string username, string pass)
        {
            try
            {
                using (var dt = new DataClassesDataContext())
                {
                    var da = (from pu in dt.Users where pu.UserName == username  && pu.Password == pass select pu).First();

                    return da;
                }
            }
            catch (Exception)
            {
                return null;
            }
        }
        #endregion
        #region[User_Update]
        public static void User_ChangePass(string Id, string Data)
        {
            try
            {
                using (var dt = new DataClassesDataContext())
                {
                    int id = int.Parse(Id);
                    //Author da = (from au in dt.Authors where au.Id == id select au).First();
                    User ob = dt.Users.Single(au => au.Id == id);                    
                    ob.Password = Data;

                    dt.SubmitChanges();
                }
            }
            catch (Exception)
            {

            }
        }
        #endregion
	}

    public class UserInfo
    {
        public string Id { get; set; }
        public string FB_Id { get; set; }
        public string UserName { get; set; }
        public string FullName { get; set; }
        public string Address { get; set; }
        public string Password { get; set; }
        public string Phone { get; set; }
        public string Email { get; set; }
        public string Status { get; set; }
        public string DateLogin { get; set; }
        public string GroupUser_Id { get; set; }

    }

}
